package com.sky.controller.admin.setmeal;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetMealController {
    @Autowired
    SetMealService setMealService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
//    为了图省事,每次修改套餐都直接删除所有套餐缓存
    void clearCache(){
        Set<String> keys = stringRedisTemplate.keys("setmealCache::*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
    }
    @PostMapping
    @ApiOperation(value = "新增套餐")
    public Result save(@RequestBody SetmealDTO dto) {
        setMealService.save(dto);
        clearCache();
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation(value = "分页查询套餐")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        return Result.success(setMealService.pageQuery(setmealPageQueryDTO));
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
    public Result<SetmealDTO> getById(@PathVariable Long id) {
        return Result.success(setMealService.getById(id));
    }
    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改套餐状态")
    public Result updateStatus(@PathVariable Integer status,Long id) {
        setMealService.updateStatus(status,id);
        clearCache();
        return Result.success();
    }
    @PutMapping
    @ApiOperation(value = "修改套餐")
    public Result update(@RequestBody SetmealDTO dto) {
        setMealService.update(dto);
        clearCache();
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    public Result delete(@RequestParam List<Long> ids) {
        setMealService.deleteBatch(ids);
        clearCache();
        return Result.success();
    }
}
